package com.fenghongzhang.day008;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    private Button write;
    private Button read;
    private Button del;
    private Button writeFile;
    private Button readFile;
    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        writeFile = (Button) findViewById(R.id.writeFile);
        readFile = (Button) findViewById(R.id.readFile);


        readFile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    FileInputStream fileInputStream = openFileInput("one.txt");
                    byte[] bytes = new byte[1];
                    int len = 0;
                    while((len = fileInputStream.read(bytes)) != -1){
                        String s = new String(bytes, 0, len);
                        Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        writeFile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    FileOutputStream fileOutputStream = openFileOutput("one.txt", MODE_PRIVATE);
                    fileOutputStream.write("hehehehe".getBytes());
                    fileOutputStream.flush();
                    fileOutputStream.close();

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });


        write = (Button) findViewById(R.id.write);
        read = (Button) findViewById(R.id.read);

        del = (Button) findViewById(R.id.del);



        write.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sharedPreferences = getSharedPreferences("1807", MODE_PRIVATE);
                SharedPreferences.Editor edit = sharedPreferences.edit();

                edit.putString("name","小明");
                edit.putBoolean("isLogin",true);
                edit.putLong("age",3l);
                edit.putFloat("age1",16f);
                edit.putInt("age2",15);

                edit.commit();

            }
        });

        read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sharedPreferences = getSharedPreferences("1807", MODE_PRIVATE);
                String name = sharedPreferences.getString("name12","大明");
                int age2 = sharedPreferences.getInt("age2", 16);
                Toast.makeText(MainActivity.this, name+age2, Toast.LENGTH_SHORT).show();
            }
        });


        del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sharedPreferences = getSharedPreferences("1807", MODE_PRIVATE);
                SharedPreferences.Editor edit = sharedPreferences.edit();
//                edit.remove("name");
                edit.clear();
                edit.commit();
            }
        });

    }

}
